package com.microblogforsim.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.microblogforsim.R;
import com.microblogforsim.fragment.Fragment_Find;
import com.microblogforsim.fragment.Fragment_Index;
import com.microblogforsim.fragment.Fragment_Message;
import com.microblogforsim.fragment.Fragment_Publish;
import com.microblogforsim.fragment.Fragment_User;
import com.microblogforsim.utils.ACache;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private static final int REQUEST_CODE_ASK_CALL_PHONE = 0;
//    底部导航栏按钮
    private LinearLayout tab_index, tab_message, tab_publish, tab_find, tab_user;
//    底部导航栏文本图标
    private TextView tv_index, tv_message, tv_find, tv_user;
//    底部导航栏图标数字
    private TextView tv_message_num,tv_user_num;
//    显示Fragment的视图
    private FrameLayout ly_show;
//    全屏视图，用于显示发布菜单
    private RelativeLayout ly_main;
//    Fragment类
    private Fragment f_index, f_message, f_publish, f_find, f_user;
//    Fragment管理器
    private FragmentManager fManager;
    //缓存
    private SharedPreferences sharedPreferences;

    private ACache mCache;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mCache = ACache.get(MainActivity.this);
        sharedPreferences = getSharedPreferences("data",MODE_PRIVATE);

        init();
      //  mCache.put("openid","1600248528");
        Intent intent = getIntent();
      int page =   intent.getIntExtra("page",0);
     switch (page){
         case 0:
             break;
         case 1:
             break;
         case 2:
             break;
         case 3:
             break;
         case 4:
             //模拟点击一次

             tab_user.performClick();
             break;
         default:
             Toast.makeText(this, "传参错误", Toast.LENGTH_SHORT).show();
             break;

     }
    }
//    初始化
    private void init() {
//        绑定组件
        tab_index = findViewById(R.id.ly_tab_menu_index);
        tab_message = findViewById(R.id.ly_tab_menu_message);
        tab_publish = findViewById(R.id.ly_tab_menu_add);
        tab_find = findViewById(R.id.ly_tab_menu_find);
        tab_user = findViewById(R.id.ly_tab_menu_user);
        tv_index = findViewById(R.id.tab_menu_index);
        tv_message = findViewById(R.id.tab_menu_message);
        tv_find = findViewById(R.id.tab_menu_find);
        tv_user = findViewById(R.id.tab_menu_user);
        tv_message_num = findViewById(R.id.tab_menu_message_num);
        tv_user_num = findViewById(R.id.tab_menu_user_num);
        ly_show = findViewById(R.id.ly_fragment);
        ly_main = findViewById(R.id.ly_main);
//        生产Fragment管理器对象
        fManager = getSupportFragmentManager();
//        点击事件监听
        tab_index.setOnClickListener(this);
        tab_message.setOnClickListener(this);
        tab_publish.setOnClickListener(this);
        tab_find.setOnClickListener(this);
        tab_user.setOnClickListener(this);
//        模拟一次点击
        tab_index.performClick();
    }
//    取消底部导航栏的选中状态
    private void deselect(){
        tv_index.setSelected(false);
        tv_message.setSelected(false);
        tv_find.setSelected(false);
        tv_user.setSelected(false);
    }
//    隐藏所有Fragment
    private void hideAllFragment(FragmentTransaction fragmentTransaction){
        if (f_index != null) fragmentTransaction.hide(f_index);
        if (f_message != null) fragmentTransaction.hide(f_message);
        if (f_find != null) fragmentTransaction.hide(f_find);
        if (f_user != null) fragmentTransaction.hide(f_user);
    }
//    所有按钮点击事件
    @Override
    public void onClick(View v) {
        FragmentTransaction fTransaction = fManager.beginTransaction();
        switch (v.getId()){
            case R.id.ly_tab_menu_index:
                hideAllFragment(fTransaction);
                deselect();
                tv_index.setSelected(true);//微博按钮选中
                if (f_index == null){//若fragment为null则创建
                    f_index = new Fragment_Index();
                    fTransaction.add(R.id.ly_fragment,f_index);
                }else fTransaction.show(f_index);//显示fragment
                break;
            case R.id.ly_tab_menu_message:
                hideAllFragment(fTransaction);
                deselect();
                tv_message.setSelected(true);
                if (f_message == null){
                    f_message = new Fragment_Message();
                    fTransaction.add(R.id.ly_fragment,f_message);
                }else fTransaction.show(f_message);
                break;
            case R.id.ly_tab_menu_add:
                if (f_publish == null) f_publish = new Fragment_Publish();
                if(sharedPreferences.getString("openid","").equals("")){
                    Toast.makeText(this, "您还未登录", Toast.LENGTH_SHORT).show();
                }else {
                    getSupportFragmentManager().beginTransaction().replace(R.id.ly_main,f_publish,"add").commit();

                }
                break;
            case R.id.ly_tab_menu_find:
                hideAllFragment(fTransaction);
                deselect();
                tv_find.setSelected(true);
                if (f_find == null){
                    f_find = new Fragment_Find();
                    fTransaction.add(R.id.ly_fragment,f_find);
                }else fTransaction.show(f_find);
                break;
            case R.id.ly_tab_menu_user:
                hideAllFragment(fTransaction);

                deselect();
                tv_user.setSelected(true);
                if (f_user == null){
                    f_user = new Fragment_User();

                    fTransaction.add(R.id.ly_fragment,f_user);
                }else fTransaction.show(f_user);
                break;
        }
        fTransaction.commit();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

}